# -*- coding: utf-8 -*-

from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from songSpider.items import SongspiderItem
from scrapy_redis.spiders import RedisCrawlSpider


class KuwoSpider(RedisCrawlSpider):
# class KuwoSpider(CrawlSpider):
    name = 'kuwo_spider'
    allowed_domains = ['kuwo.cn']
    # start_urls = ['http://yinyue.kuwo.cn/yy/category.htm',
    #               'http://sou.kuwo.cn/ws/NSearch?type=all&catalog=yueku20177&key=%E5%85%84%E5%BC%9F%E6%8A%B1%E4%B8%80%E4%B8%8B',
		# 		  'http://sou.kuwo.cn/ws/NSearch?type=all&catalog=yueku20177&key=%E7%97%85%E5%8F%98',
		# 		  'http://sou.kuwo.cn/ws/NSearch?type=music&key=%E5%BC%A0%E5%AD%A6%E5%8F%8B',
    #               'http://yinyue.kuwo.cn/yy/cinfo_24153.htm',
    #               'http://sou.kuwo.cn/ws/NSearch?key=%E4%B8%AD%E5%9B%BD%E6%9C%80%E5%BC%BA%E9%9F%B3&type=all',
    #               'http://www.kuwo.cn/mingxing',
    #               'http://sou.kuwo.cn/ws/NSearch?type=playlist&key=%E7%9B%97%E5%B0%86%E8%A1%8C',
    #               ]

    redis_key = "kuwo:start_url"
    # 设置爬取规则,可迭代对象，可设置多个规则
    rules = [

            Rule(LinkExtractor(allow=(
                                  "http://www.kuwo.cn/yinyue/(\d+)")),
             callback='get_song', follow=True),
            Rule(LinkExtractor(allow=(
                                  "http://www.kuwo.cn/bang/(.*)"))),
            Rule(LinkExtractor(allow=(
                                  ".*"))),
            Rule(LinkExtractor(allow=(
                                  "(.*)=yueku(\d+)"))),
            Rule(LinkExtractor(allow=(
                                  "http://sou.kuwo.cn/ws(.*)"))),
			Rule(LinkExtractor(allow=(
                                  "http://sou.kuwo.cn/ws/NSearch?type=music&key=(.*)"))),

            Rule(LinkExtractor(allow=("http://antiserver.kuwo.cn/anti.s(.*)"))),
            Rule(LinkExtractor(allow=("http://yinyue.kuwo.cn(.*)"))),
            Rule(LinkExtractor(allow=("http://www.kuwo.cn/artist/(.*)"))),
            Rule(LinkExtractor(allow=("http://yinyue.kuwo.cn/yy(.*)"))),
            Rule(LinkExtractor(allow=("http://sou.kuwo.cn/ws/NSearch?type=all&catalog=(.*)&key="))),
             ]

    # 获取歌曲信息
    def get_song(self,response):

        item = SongspiderItem()
        item['name'] = response.xpath('//*[@id="lrcName"]/text()').extract()[0]
        item['singer'] = response.xpath('//*[@id="musiclrc"]/div[1]/p[2]/span/a/text()').extract()[0]
        # lyric = # 歌词
        lyrics = response.xpath('//*[@id="llrcId"]/p')

        lyricstr = item['name']
        for lyric in lyrics:
            lyricstr += '\n'
            lyricstr += lyric.xpath('text()').extract()[0]
        item['lyric'] = lyricstr
        item['url'] = response.url
        item['originId'] = 2
        # print('歌曲信息：',item['name'],item['singer'],item['lyric'],item['url'])
        # with open(r'D:\Learn\pythonPro\qianfeng\MusicSearch\songSpider\songs.csv', 'a', newline='',encoding='utf-8')  as f:
        #     writer = csv.writer(f)
        #     # 按行写入
        #     writer.writerow([item['name'],item['singer'],item['lyric'],item['url'],])
        return item

